<script setup>
// import { getSDK } from "open-im-sdk-wasm";
import { getSDK } from "../utils/lib";
// const OpenIM = getSDK('./openIM.wasm');
const OpenIM = getSDK();
import { onMounted } from 'vue'
import {UserService} from '../http/api'

// import GoEasy from 'goeasy'
// let goeasy = GoEasy.getInstance({
//     host:"hangzhou.goeasy.io",  //若是新加坡区域：singapore.goeasy.io
//     appkey:"BC-5c04ddc64127467a85d4e888a271c8f4",
//     modules:['im']//根据需要，传入'im’或‘pubsub’，或数组方式同时传入
// });

defineProps({
  msg: {
    type: String,
    required: true
  }
})
function greet(params) {
  // const config = {
  //   userID: "6536782126",
  //   token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySUQiOiI2NTM2NzgyMTI2IiwiUGxhdGZvcm1JRCI6NSwiZXhwIjoxNjk5ODQyMjgyLCJuYmYiOjE2OTIwNjU5ODIsImlhdCI6MTY5MjA2NjI4Mn0.yhEkGKVu0_Ty8tb37cfJr2AC88zG3oc0siFHrjtz5_M",
  //   // apiAddress: "http://47.106.236.38:10002",
  //   wsAddress: "ws://47.106.236.38:10001",
  //   platformID: 5,
  // };
  const config = {
    userID: params.userID,
    token: params.imToken,
    apiAddr: "http://47.106.236.38:10002",
    wsAddr: 'ws://47.106.236.38:10001',
    platformID: 5,
  };
  // const config = {
  //   userID: params.userID,
  //   token: params.imToken,
  //   platformID: 5,
  //   wsAddr: 'ws://47.106.236.38:10001',
  // };
  console.log(config)
  OpenIM.login(config).then(res => {
    console.log("1");
    OpenIM.getLoginStatus().then(res => {
      // 101:登录成功 102:登陆中 103:登录失败 201:登出
      console.log(res)
    }).catch(err => {
      console.log("2");
    })
    OpenIM.getLoginUser().then(res => {
      console.log("3");
      // 当前登录用户ID
    }).catch(err => {
      console.log("4");
    })
  }).catch(err => {
    console.log(err)
    console.log("login failed...");
  })
}
onMounted(()=> {
  setTimeout(() => {
    // greet()
    // login1()
    login4()
  }, 6000);
  // greet()
  // login1()
  // connect()
})
const login1 = async () => {
  const loginParams = {
    mobile: '18521737401',
    code: '0000',
    invite_code: ''
  }
  const res = await UserService.login1(loginParams)
  console.log(res)
  let params = res.data.data
  greet(params)
}
const login4 = async () => {
  // const loginParams = {
  //   mobile: '18521737401',
  //   code: '0000',
  //   invite_code: ''
  // }
  const loginParams = {
    page: 1,
    per_page: 3
  }
  const res = await UserService.login4(loginParams)
  console.log(res)
}
function connect() {
  // 建立连接
  goeasy.connect({
        id:"001", //im必填，最大长度60字符
        data:{"avatar":"/www/xxx.png","nickname":"Neo"}, //必须是一个对象，im必填，最大长度300字符，显示在会话列表中
        onSuccess: function () {  //连接成功
          console.log("GoEasy connect successfully.") //连接成功
        },
        onFailed: function (error) { //连接失败
          console.log("Failed to connect GoEasy, code:"+error.code+ ",error:"+error.content);
        },
        onProgress:function(attempts) { //连接或自动重连中
          console.log("GoEasy is connecting", attempts);
        }
    });
}
</script>

<template>
  <div class="greetings">
    <h1 class="green">{{ msg }}</h1>
    <el-button type="primary">
      <router-link to="/">index</router-link>
    </el-button>
    <el-button type="primary">
      <router-link to="/home">home</router-link>
    </el-button>
    <el-button type="danger">
      <router-link to="/blog">blog</router-link>
    </el-button>
  </div>
</template>

<style scoped>
h1 {
  font-weight: 500;
  font-size: 2.6rem;
  position: relative;
  top: -10px;
}


.greetings h1,
.greetings h3 {
  text-align: center;
}

@media (min-width: 1024px) {
  .greetings h1,
  .greetings h3 {
    text-align: left;
  }
}
</style>
